<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:pp="http://expath.org/ns/project"
                xmlns:zip="http://expath.org/ns/zip"
                exclude-result-prefixes="#all"
                version="2.0">

   <xsl:import href="http://expath.org/ns/project/package.xsl"/>

   <!--
       Add the JARs from {java-prj}/dist and {java-prj}/lib to the package.
   -->

   <!-- The overload point. -->
   <xsl:template match="zip:file" mode="pp:modify-package">
      <xsl:apply-templates select="." mode="add-jars"/>
   </xsl:template>

   <!-- Copy everything... -->
   <xsl:template match="node()" mode="add-jars">
      <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates select="node()" mode="add-jars"/>
      </xsl:copy>
   </xsl:template>

   <!-- ..except garbage... -->
   <xsl:template match="zip:dir[@name eq '.~']" mode="add-jars"/>

   <!-- ...except the existing (and empty) 'spec' directory. -->
   <xsl:template match="zip:dir[@name eq 'gae-tools']" mode="add-jars">
      <!-- the path to the Java project dir -->
      <xsl:variable name="java-prj" select="resolve-uri('../java/gae-tools/', $pp:project)"/>
      <xsl:copy>
         <!-- copy existing stuff -->
         <xsl:copy-of select="@*, *"/>
         <!-- then add the JAR from {java-prj}/dist/ -->
         <zip:dir name="jar">
            <zip:entry src="{ resolve-uri('dist/gae-tools.jar', $java-prj) }"/>
         </zip:dir>
         <!-- then add the JARs from {java-prj}/lib/ -->
         <zip:dir name="lib">
            <xsl:sequence select="pp:zip-directory-content(resolve-uri('lib/', $java-prj))"/>
         </zip:dir>
      </xsl:copy>
   </xsl:template>

</xsl:stylesheet>
